﻿using System;
using System.ComponentModel.DataAnnotations;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace StudySL4BizApp.Web
{
    public partial class Order_Detail
    {
        private ValidationResult quantityUnAvailable;
        private ValidationResult productInExists;

        public void Validate()
        {
            OnQuantityChanged();
            if (HasValidationErrors)
            {
                return;
            }

            OnProductIDChanged();
        }

        partial void OnQuantityChanged()
        {
            if (IsDeserializing)
                return;

            var context = new Services.NorthWindContext();
            context.IsProductInStock(this.ProductID, this.Quantity,
                                     operation =>
                                     {
                                         if (!operation.Value)
                                         {
                                             quantityUnAvailable =
                                                 new ValidationResult("Product out of stock",
                                                                      new string[] { "Quantity" });
                                             ValidationErrors.Add(quantityUnAvailable);
                                         }
                                         else
                                         {
                                             if (quantityUnAvailable != null)
                                             {
                                                 ValidationErrors.Remove(quantityUnAvailable);
                                             }
                                         }
                                     }, null);
        }

        partial void OnProductIDChanged()
        {
            var context = new Services.NorthWindContext();
            context.ProductExists(ProductID, operation =>
                                                 {
                                                     if (!operation.Value)
                                                     {
                                                         productInExists = new ValidationResult(
                                                             "Product doesn't exists", new string[] { "ProductID" });
                                                         ValidationErrors.Add(productInExists);
                                                     }
                                                     else
                                                     {
                                                         if (productInExists != null)
                                                         {
                                                             ValidationErrors.Remove(productInExists);
                                                         }
                                                     }
                                                 }, null);

            OnQuantityChanged();
        }
    }
}
